import { bootEvent } from '../boot';
import { makeLabelLogger } from '../util/logger';
import * as express from 'express';
import { NewGuestActionType } from 'src/service/api/user';
import { msgEvent, redisXAdd, syncRedisCommon, publishMessage, DataCenterUrl } from './smain';
import fetch from 'node-fetch';
import { SyncUserMessageType } from 'src/service/api/sync';

let { llog, lerror } = makeLabelLogger('mbsync-user');

msgEvent.on('NewUser', async (action: NewGuestActionType) => {
	llog('forwardMessage', action);
	try {
		let r = await publishMessage(SyncUserMessageType, action);
	} catch (e) {
		lerror('forwardMessage fail', e);
	}
});

msgEvent.on('NewGuest', async action => {
	llog('forwardMessage', action);
	try {
		let r = await publishMessage(SyncUserMessageType, action);
	} catch (e) {
		lerror('forwardMessage fail', e);
	}
});

msgEvent.on('UpdateViewNums', async action => {
	llog('forwardMessage', action);
	try {
		let r = await publishMessage(SyncUserMessageType, action);
	} catch (e) {
		lerror('forwardMessage fail', e);
	}
});

msgEvent.on('notify', async action => {
	// llog('get notify', action);
	if (action.info.type == 'visitor') {
		let deviceInfo = JSON.parse(action.info.deviceInfo);
		notify('visitor', {
			clientid: action.info.id,
			visitorip: action.info.clientIP,
			types: deviceInfo.Manufacturer + '/' + deviceInfo.Model,
			sys: deviceInfo.Platform,
			hardware: deviceInfo.Id,
			screen: deviceInfo.ScreenWidth + 'x' + deviceInfo.ScreenHeight,
			hardInfo: action.info.deviceInfo,
		});
	} else if (action.info.type == 'register') {
		notify('account-reg', {
			clientid: action.info.id,
			regip: action.info.clientIP,
			tel: action.info.phone,
			nickname: action.info.name,
		});
	} else if (action.info.type == 'login') {
		notify('account-login', {
			clientid: action.info.id,
			visitorid: action.info.extra.lastid,
			loginip: action.info.clientIP,
		});
	} else if (action.info.type == 'clickad') {
		notify('image-update', {
			clientid: action.info.id,
			imgid: action.info.extra.imgid,
		});
	} else if (action.info.type == 'views') {
		notify('account-views', {
			clientid: action.info.id,
			filmid: action.info.extra.movieId,
			filmname: action.info.extra.movieName,
			viewtime: action.info.extra.viewTime,
			loadtime: action.info.extra.loadTime,
			viewduration: action.info.extra.viewDuration,
		});
	}
});

let notify = async (type, param) => {
	let url = `${DataCenterUrl}/${type}`;
	let body = JSON.stringify(param);
	llog('postnotify', url, param);
	let text;
	try {
		let res = await fetch(url, {
			method: 'post',
			body,
			headers: { 'Content-Type': 'application/json' },
			timeout: 1000 * 15,
		});
		text = await res.text();
		let json = JSON.parse(text);
		if (!json.status) {
			throw new Error('status not true');
		}
	} catch (e) {
		llog('postnotify fail', e);
		syncRedisCommon.lpush(
			'notify:errors',
			JSON.stringify({
				url,
				body,
				text,
				error: e.toString(),
				time: Date.now(),
			}),
		);
	}
};
